package com.luzhikun.path.com.luzhikun.myExamSystem.controller;
/**
 * ClassName: ${NAME}
 * Description: ${Description}
 * date: 2021/4/22 23:11
 *
 * @author luzhikun
 * @version
 * @since JDK 1.8
 */

import com.luzhikun.path.com.luzhikun.myExamSystem.dao.UserDao;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 用户删除
 */
public class UserDeleteServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //创建Dao层对象
        UserDao userDao = new UserDao();
        //创建输出流
        PrintWriter out = null;
        String userId;
        //获取结果
        boolean isFlag;
        //1.【调用请求对象】读取【请求头】参数(用户变好)
        userId = request.getParameter("userid");
        System.out.println(userId);
        //判断传入的id是否为空或者为空串 如果为空串就 返回0
        int uId = userId != null && !userId.equals("") ? Integer.parseInt(userId) : 0;
        //2.【调用Dao】将用户编号填充到delete命令并发送到数据库服务器
        isFlag = userDao.deleteUser(uId);
        //改变字符集
        response.setContentType("text/html;charset=UTF-8");
        //3.[调用响应对象]讲处理结果以二进制写入到响应体中，交给浏览器
        out = response.getWriter();
        if (isFlag) {
            out.println("删除成功");
        } else {
            out.println("删除失败");
        }


    }

}
